package com.chenjt.juc.four_lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/7/6 16:59
 */


class Ticket{

    private int number = 50;

    ReentrantLock lock = new ReentrantLock(true);

    public void sale(){
       lock.lock();
        try {
            if(number > 0){
                System.out.println(Thread.currentThread().getName()
                +"卖出第：\t" + number-- + "\t 还剩下：" + number);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}

public class SaleTicketDemo {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(()->{
            for(int i = 0;i<55;i++){
                ticket.sale();
            }
        },"a").start();

        new Thread(()->{
            for(int i = 0;i<55;i++){
                ticket.sale();
            }
        },"b").start();

        new Thread(()->{
            for(int i = 0;i<55;i++){
                ticket.sale();
            }
        },"c").start();



    }

}
